qx.Class.define("caraffa.forms.Pagares",
{
extend : qx.ui.form.ow.FormTab,
construct : function (fecha)
{
	this.base(arguments, "Pagares", "Pagares");
	this._fecha = fecha;
	//RPC
	this._rpcPag = new qx.io.remote.Rpc();
	this._rpcPag.setTimeout(10000);
	this._rpcPag.setUrl("services/");
	this._rpcPag.setServiceName("Pagares");
	
	
	this._grpPagares = new qx.ui.groupbox.GroupBox();
		this._grpPagares.setLayout(new qx.ui.layout.Basic());
		var strMostrar = [{value:"1", label:"Vencidos"},{value:"2", label:"Por Vencer"}, {value:"3", label:"Todos"}];
		this._rdbMostrar = new qx.ui.form.ow.Radios("Mostrar:",strMostrar,true);
		this._txtPaciente = new qx.ui.form.ow.TextField("Paciente:");
			this._txtPaciente.setValue("");

		this._mdlPagares = new qx.ui.table.model.Simple();
			this._mdlPagares.setColumns(["Código","DNI","Nombre", "Prestación","Día", "Vencimiento", "Importe", "Pagado", "Usuario"]);
		this._tblPagares = new qx.ui.table.Table(this._mdlPagares);
			this._tblPagares.setStatusBarVisible(false);
			this._tblPagares.setColumnWidth(0, 70);
				this._tblPagares.getTableColumnModel().setColumnVisible(0,false);
			this._tblPagares.setColumnWidth(1, 100);
			this._tblPagares.setColumnWidth(2, 200);
			this._tblPagares.setColumnWidth(3, 200);
			this._tblPagares.setColumnWidth(4, 90);
			this._tblPagares.setColumnWidth(5, 90);
			this._tblPagares.setColumnWidth(6, 100);
			this._tblPagares.setColumnWidth(7, 100);
			this._tblPagares.setColumnWidth(8, 80);
				this._tblPagares.getTableColumnModel().setColumnVisible(8,false);
			this._tblPagares.setShowCellFocusIndicator(false);
			this._tblPagares.setWidth(930);
			this._tblPagares.setHeight(350);

	this._grpBotonera = new qx.ui.groupbox.GroupBox();
		this._grpBotonera.setWidth(350);
		this._grpBotonera.setLayout(new qx.ui.layout.HBox(20).set({alignX:"center"}));
		this._btnCobrarPagare = new qx.ui.form.Button("Cobrar");
	this._grpBotonera.add(this._btnCobrarPagare);
			
	this._grpPagares.add(this._rdbMostrar,{top:0,left:0});
	this._grpPagares.add(this._txtPaciente,{top:30,left:0});
	this._grpPagares.add(this._tblPagares,{top:60,left:0});
	this._grpPagares.add(this._grpBotonera,{top:430,left:250});
	
	this.add(this._grpPagares,{top:0,left:0});
	this._attachEvents();	
},

members: {
	
	_attachEvents : function (){
		this.addListener('appear', this._initForm, this);
		this._txtPaciente.getText().addListener("input", this._getPagares, this);
		this._rdbMostrar.addListener('changeValue', this._getPagares, this);
		this._btnCobrarPagare.addListener("execute", this._cobrarPagare, this);
	},
	_initForm : function(){
		this._getPagares();
	},
	_getPagares : function ()
	{
		if (this._txtPaciente.getValue().length >= 3)
		{
			try
			{
				var params = new Object();
				params.mostrar = this._rdbMostrar.getValue();
				params.pacienteBuscar = this._txtPaciente.getValue();
				if(! isNaN(parseInt(params.pacienteBuscar))){
					params.pacienteFiltrarPor = "D";
				}
				else{
					params.pacienteFiltrarPor = "N";
				}
				var resPagares = this._rpcPag.callSync("getPagares", params);
				if(resPagares != null){
					this._tblPagares.getTableModel().setData(resPagares.values);
				}
				else{
					alert("No hay pagares con el filtro seleccionado");
				}
			}
			catch (ex)
			{
				alert(ex);
			}
		}
		// mostrar todos si no se agrego un filtro
		else if(this._txtPaciente.getValue().length == 0){
			try
			{
				var params = new Object();
				params.mostrar = this._rdbMostrar.getValue();
				params.pacienteBuscar = this._txtPaciente.getValue();
				params.pacienteFiltrarPor = "D";
				var resPagares = this._rpcPag.callSync("getPagares", params);
				if(resPagares != null){
					this._tblPagares.getTableModel().setData(resPagares.values);
				}
				else{
					alert("No hay pagares con el filtro seleccionado");
				}
			}
			catch (ex)
			{
				alert(ex);
			}
		}
	},
	_cobrarPagare: function(){
		if(confirm("Cobrar Pagaré?")){
			try
			{
				var params = new Object();
					params.cod_pagare = this._mdlPagares.getValue(0, this._tblPagares.getFocusedRow());
					var resPago = this._rpcPag.callSync("cobrarPagare", params);
					this._getPagares();
			}
			catch (ex)
			{
				alert(ex);
			}
		}
		else{
				
		}
	}
}
});
